﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Text;
using System.Drawing.Imaging;
using System.IO;
using System.Security.Cryptography;
using util;
using System.IO.Compression;
using MySQLDriverCS;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        //[STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            //Bitmap bb = getScreen();
            //img2file(bb,"test1.jpg");

            //Console.Write(GetPixelColor(0,0));

            //string value = readini("group1", "key1", "default_value1", ".\\set.ini");
            //writeini("group2", "key2", value, ".\\set.ini");

            //Bitmap b = file2img("test1.jpg");
            //Bitmap bb = img_alpha(b, 200);
            //img2file(bb, "test1.png");

            //Bitmap b = file2img("test1.jpg");
            //Bitmap bb = img_tailor(b, new Rectangle(50, 50, 50, 50));
            //img2file(bb, "test12.jpg");

            //Bitmap b = file2img("test1.jpg");
            //Bitmap bb = img_color_brightness(b, 100);
            //img2file(bb, "img_color_brightness.jpg");

            //Color[,] allcolor = img2color("test1.jpg");

            //for (int i = 0; i < allcolor.GetLength(0); i++)
            //{
            //    for (int j = 0; j < allcolor.GetLength(1); j++)
            //    {
            //        System.Console.WriteLine(allcolor[i, j]);
            //    }
            //}
            //MessageBox.Show("tang..... " + allcolor);

            //file_classify_format(@"C:\Users\Administrator\Desktop\CreatorClient\Client\local", @"C:\Users\Administrator\Desktop\000");

            //MyEncrypt.SHA_Encrypt("test.txt", "test1.txt", "123456");  //文件加密
            //MyEncrypt.SHA_Dencrypt("test1.txt", "test2.txt", "123456");  //文件解密

            //DataTable temp =  Query_Database("localhost","nodetest","root","tang1990", "");

            //Bitmap b = image_class.splice_image(false, new Size(720, 1280),new Bitmap[] { file2img("test1.jpg"), file2img("test1.jpg"), file2img("test1.jpg") }); //file2img("test1.jpg");
            //img2file((Bitmap)b, "splice_image.png");

            //Image b = image_class.scale_image(file2img("test1.jpg"), 100,100);
            //img2file((Bitmap)b, "scale_image.png");

            //Image b = image_class.scale_image(file2img("test1.jpg"),1.5f);
            //img2file((Bitmap)b, "scale_image2.png");

            //Image b = image_class.rect_image(file2img("test1.jpg"),new Point(0,0),new Point(100,100),new Pen(Brushes.Red));
            //img2file((Bitmap)b, "rect_image.png");

            //Image b = image_class.ellipse_image(file2img("test1.jpg"), new Point(0, 0), new Point(100, 100),new Color());
            //img2file((Bitmap)b, "ellipse_image.png");

            Image b = image_class.color_image(file2img("test1.jpg"));
            img2file((Bitmap)b, "color_image.png");

            //pictureBox1 = image_class.show_scale();
        }

        #region 获取屏幕截图
        public static Bitmap getScreen()
        {
            Bitmap baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(baseImage);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            g.Dispose();
            return baseImage;
        }
        public static void img2file(Bitmap b, string filepath)
        {
            b.Save(filepath);
        }
        #endregion

        #region 获取屏幕指定坐标点的颜色
        private struct POINT
        {
            private int x;
            private int y;
        }

        static POINT point;

        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int GetDC(int hwnd);
        [DllImport("gdi32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int GetPixel(int hdc, int x, int y);
        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int ReleaseDC(int hwnd, int hdc);
        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int WindowFromPoint(int x, int y);
        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int ScreenToClient(int hwnd, ref POINT lppoint);

        //获取屏幕指定坐标点的颜色
        public static Color GetPixelColor(int x, int y)
        {
            int h = WindowFromPoint(x, y);
            int hdc = GetDC(h);

            ScreenToClient(h, ref point);
            int c = GetPixel(hdc, x, y);
            return Color.FromArgb(c);
        }
        #endregion

        #region 读取ini配置数据、将数据存储至ini文件
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 读取ini
        /// </summary>
        /// <param name="group">数据分组</param>
        /// <param name="key">关键字</param>
        /// <param name="filepath">init文件地址</param>
        /// <returns>关键字对应的值，没有时含有默认值</returns>
        public static string readini(string group, string key, string default_value, string filepath)
        {
            StringBuilder temp = new StringBuilder();
            GetPrivateProfileString(group, key, default_value, temp, 255, filepath);
            return temp.ToString();
        }

        /// <summary>
        /// 存储ini
        /// </summary>
        /// <param name="group">数据分组</param>
        /// <param name="key">关键字</param>
        /// <param name="value">关键字对应的值</param>
        /// <param name="filepath">ini文件地址</param>
        public static void writeini(string group, string key, string value, string filepath)
        {
            WritePrivateProfileString(group, key, value, filepath);
        }
        #endregion

        #region 改变图片透明度，需要保存成png格式，透明度参数alpha的取值范围为0-255
        public static Bitmap img_alpha(Bitmap src, int alpha)
        {
            Bitmap bmp = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            for (int h = 0; h < src.Height; h++)
                for (int w = 0; w < src.Width; w++)
                {
                    Color c = src.GetPixel(w, h);
                    bmp.SetPixel(w, h, Color.FromArgb(alpha, c.R, c.G, c.B));//色彩度最大为255，最小为0
                }
            return bmp;
        }

        public static Bitmap file2img(string filepath) {
            Bitmap b = new Bitmap(filepath);
            return b;
        }
        #endregion

        #region 图片裁剪函数
        public static Bitmap img_tailor(Bitmap src, Rectangle range)
        {
            return src.Clone(range, System.Drawing.Imaging.PixelFormat.DontCare);
        }
        #endregion

        #region 图片亮度调整
        //public static unsafe Bitmap img_color_brightness(Bitmap src, int brightness)
        //{
        //    int width = src.Width;
        //    int height = src.Height;
        //    Bitmap back = new Bitmap(width, height);
        //    Rectangle rect = new Rectangle(0, 0, width, height);
        //    //这种速度最快
        //    //24位rgb显示一个像素，即一个像素点3个字节，每个字节是BGR分量。Format32bppRgb是用4个字节表示一个像素
        //    BitmapData bmpData = src.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        //    byte* ptr = (byte*)(bmpData.Scan0);
        //    for (int j = 0; j < height; j++)
        //    {
        //        for (int i = 0; i < width; i++)
        //        {
        //            //ptr[2]为r值，ptr[1]为g值，ptr[0]为b值
        //            int red = ptr[2] + brightness; if (red > 255) red = 255; if (red < 0) red = 0;
        //            int green = ptr[1] + brightness; if (green > 255) green = 255; if (green < 0) green = 0;
        //            int blue = ptr[0] + brightness; if (blue > 255) blue = 255; if (blue < 0) blue = 0;
        //            back.SetPixel(i, j, Color.FromArgb(red, green, blue));
        //            ptr += 3; //Format24bppRgb格式每个像素占3字节
        //        }
        //        ptr += bmpData.Stride - bmpData.Width * 3;//每行读取到最后“有用”数据时，跳过未使用空间XX
        //    }
        //    src.UnlockBits(bmpData);
        //    return back;
        //}
        #endregion

        #region 读取图片成rgb二维数组，输入图片文件地址，输出颜色二维数组
        public static Color[,] img2color(String imgfile)
        {
            Bitmap img = new Bitmap(imgfile);
            Color[,] allcolor = new Color[img.Height, img.Width];
            for (int h = 0; h < img.Height; h++)
            {
                for (int w = 0; w < img.Width; w++)
                {
                    allcolor[h, w] = img.GetPixel(w, h);
                }
            }

            GC.Collect();
            //System.Console.WriteLine(allcolor);
            return allcolor;
        }
        #endregion

        #region 文件批处理：剪切、复制、删除、改名、分类等
        /// <summary>
        /// 文件剪切、改名操作
        /// </summary>
        /// <param name="srcdir">源文件夹</param>
        /// <param name="desdir">目的文件夹</param>
        public static void file_cut(string srcdir,string desdir) {
            DirectoryInfo thefolder = new DirectoryInfo(srcdir);
            foreach (FileInfo nextfile in thefolder.GetFiles()) {
                try {
                    string filename = nextfile.Name;
                    string filefullname = nextfile.FullName;
                    string mudi = desdir + "\\" + filename;
                    File.Move(filefullname,mudi);
                } catch (System.Exception ex) {
                    System.Console.WriteLine(ex.ToString());
                }
            }
        }

        /// <summary>
        /// 文件复制函数
        /// </summary>
        /// <param name="srcdir">源文件夹</param>
        /// <param name="desdir">目的文件夹</param>
        public static void file_copy(string srcdir,string desdir) {
            DirectoryInfo thefolder = new DirectoryInfo(srcdir);
            foreach (FileInfo nextfile in thefolder.GetFiles()) {
                try{
                    string filename = nextfile.Name;
                    string filefullname = nextfile.FullName;
                    string mudi = desdir + "\\" + filename;
                    if (File.Exists(mudi))
                    {
                        File.Delete(mudi);
                    }
                    else {
                        File.Copy(filefullname,mudi);
                    }
                } catch (System.Exception ex) {
                    System.Console.WriteLine(ex.ToString());
                }
            }
        }

        /// <summary>
        /// 删除文件函数
        /// </summary>
        /// <param name="srcdir"></param>
        public static void file_delete(string srcdir) {
            DirectoryInfo thefolder = new DirectoryInfo(srcdir);
            foreach (FileInfo nextfile in thefolder.GetFiles()) {
                try{
                    string filenmae = nextfile.Name;
                    string filefullname = nextfile.FullName;
                    File.Delete(filefullname);
                } catch (Exception ex) {
                    Console.WriteLine(ex);
                }
            }
        }

        /// <summary>
        /// 文件按时间分类到不同的文件夹
        /// </summary>
        /// <param name="srcdir">源文件夹</param>
        /// <param name="desdir">目的文件夹</param>
        public static void file_classify_date(string srcdir,string desdir) {
            DirectoryInfo thefolder = new DirectoryInfo(srcdir);
            foreach (FileInfo nextfile in thefolder.GetFiles()) {
                try{
                    string filename = nextfile.Name;
                    string filefullname = nextfile.FullName;
                    string creattime = nextfile.CreationTime.Year.ToString()+"_"+nextfile.CreationTime.Month.ToString()+"_"+nextfile.CreationTime.Day.ToString();
                    string desdirtemp = desdir + "\\" + creattime;
                    if (!Directory.Exists(desdirtemp)) {
                        Directory.CreateDirectory(desdirtemp);
                    }
                    string mudifile = desdirtemp + "\\" + filename;
                    if (File.Exists(mudifile))
                    {
                        File.Delete(mudifile);
                    }
                    else {
                        File.Move(filefullname,mudifile);
                    }
                }catch (Exception ex) {
                    Console.WriteLine(ex);
                }
            }
        }

        /// <summary>
        /// 文件按格式分类到不同的文件夹
        /// </summary>
        /// <param name="srcdir">源文件夹</param>
        /// <param name="desdir">目的文件夹</param>
        public static void file_classify_format(string srcdir,string desdir) {
            DirectoryInfo thefolder = new DirectoryInfo(srcdir);
            foreach (FileInfo nextfile in thefolder.GetFiles()) {
                try {
                    string filename = nextfile.Name;
                    string filefullname = nextfile.FullName;
                    string filetype = Path.GetExtension(filefullname);
                    string desdirtemp = desdir + "\\" + filetype;
                    if (!Directory.Exists(desdirtemp)) {
                        Directory.CreateDirectory(desdirtemp);
                    }
                    string mudifile = desdirtemp + "\\" + filename;
                    if (File.Exists(mudifile))
                    {
                        File.Delete(mudifile);
                    }
                    else {
                        File.Move(filefullname,mudifile);
                    }
                } catch (Exception e) {
                    Console.WriteLine(e.ToString());
                }
            }
        }

        /// <summary>
        /// 所有文件名添加指定模式的字符串
        /// </summary>
        /// <param name="dirpath">源文件夹</param>
        /// <param name="set">修改模式</param>
        /// <param name="append">名称是追加还是修改</param>
        public static void file_name_change(string dirpath,string set,bool append) {
            if (set=="") {
                throw new Exception("修改文件名不能为空");
                return;
            }

            DirectoryInfo thefolder = new DirectoryInfo(dirpath);
            int index = 0;
            foreach (FileInfo nextfile in thefolder.GetFiles()) {
                try {
                    string filename = nextfile.Name;
                    string filefullname = nextfile.FullName;
                    string yuandir = filefullname.Substring(0,filefullname.LastIndexOf("\\"));
                    string filetype = Path.GetExtension(filefullname);
                    filename = filename.Replace(filetype,"");
                    if (!append) {
                        filename = "";
                    }
                    if (set == "date")
                    {
                        string mudifile = yuandir + "\\" + filename + DateTime.Now.ToString("yyyy-MM-dd");
                        Directory.Move(filefullname, mudifile);
                    }
                    else if (set == "time")
                    {
                        string mudifile = yuandir = "\\" + filename + DateTime.Now.ToString("dd-mm-dd-mm-ss") + filetype;
                        Directory.Move(filefullname, mudifile);
                    }
                    else if (set == "number")
                    {
                        string mudifile = yuandir + "\\" + filename + Convert.ToString(index) + filetype;
                        index++;
                        Directory.Move(filefullname, mudifile);
                    }
                    else {
                        string mudifile = yuandir + "\\" + filename + set + filetype;
                        Directory.Move(filefullname,mudifile);
                    }
                } catch (Exception e) {
                    Console.WriteLine(e.ToString());
                }
            }
        }
        #endregion

        #region 访问mysql数据库
        public static String ExecCommand(string host,string database,string user,string password,string sqlcommand) {
            try {
                MySQLDriverCS.MySQLConnection conn = new MySQLDriverCS.MySQLConnection(new MySQLDriverCS.MySQLConnectionString(host, database, user, password).AsString);
                conn.Open();
                MySQLDriverCS.MySQLCommand comm = new MySQLDriverCS.MySQLCommand("set names gb2312", conn);
                comm.ExecuteNonQuery();
                MySQLDriverCS.MySQLCommand mysqlcommand = new MySQLDriverCS.MySQLCommand(sqlcommand,conn);
                mysqlcommand.ExecuteNonQuery();
                conn.Close();
            } catch (Exception e) {
                MessageBox.Show(e.ToString());
            }
            return "";
        }

        public static System.Data.DataTable Query_Database(string host, string database, string user, string password, string sqlcommand)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            try
            {
                MySQLConnection conn = new MySQLConnection(new MySQLConnectionString(host, database, user, password).AsString);  //构造连接数据库字符串
                conn.Open();//打开连接
                            //防止乱码
                MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
                commn.ExecuteNonQuery();
                MySQLDataAdapter mda = new MySQLDataAdapter(sqlcommand, conn);

                DataSet ds = new DataSet();
                mda.Fill(ds, "result");
                dt = ds.Tables["result"];
                conn.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return dt; //返回数据集，用于绑定控件作为数据源
        }
        #endregion
    }
}


#region SHA256算法实现对文件的加密和解密
namespace util
{
    public class MyEncrypt
    {
        private const ulong FC_TAG = 0xFC010203040506CF;
        private const int BUFFER_SIZE = 128 * 1024;
        private static bool CheckByteArrays(byte[] b1, byte[] b2)
        {
            if (b1.Length == b2.Length)
            {
                for (int i = 0; i < b1.Length; i++)
                {
                    if (b1[i] != b2[i])
                    {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

        private static SymmetricAlgorithm CreateRijndael(string password, byte[] salt)
        {
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000);
            SymmetricAlgorithm sma = Rijndael.Create();
            sma.KeySize = 256;
            sma.Key = pdb.GetBytes(32);
            sma.Padding = PaddingMode.PKCS7;
            return sma;
        }
        private static RandomNumberGenerator rand = new RNGCryptoServiceProvider();
        private static byte[] GeneraterRandomBytes(int count)
        {
            byte[] bytes = new byte[count];
            rand.GetBytes(bytes);
            return bytes;
        }

        //加密文件
        public static void SHA_Encrypt(string infile, string outfile, string password)
        {
            using (FileStream fin = File.OpenRead(infile), fout = File.OpenWrite(outfile))
            {
                long lsize = fin.Length;
                int size = (int)lsize;
                byte[] bytes = new byte[BUFFER_SIZE];
                int read = -1;
                int value = 0;
                int outValue = 0;
                byte[] iv = GeneraterRandomBytes(16);
                byte[] salt = GeneraterRandomBytes(16);
                SymmetricAlgorithm sma = CreateRijndael(password, salt);
                sma.IV = iv;
                fout.Write(iv, 0, iv.Length);
                fout.Write(salt, 0, salt.Length);
                HashAlgorithm hasher = SHA256.Create();
                using (CryptoStream cout = new CryptoStream(fout, sma.CreateEncryptor(), CryptoStreamMode.Write), chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
                {
                    BinaryWriter bw = new BinaryWriter(cout);
                    bw.Write(lsize);
                    bw.Write(FC_TAG);
                    while ((read = fin.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        cout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value += read;
                    }
                    chash.Flush();
                    chash.Close();
                    byte[] hash = hasher.Hash;
                    cout.Write(hash, 0, hash.Length);
                    cout.Flush();
                    cout.Close();
                }
            }
        }

        // 解密文件 
        public static void SHA_Dencrypt(string infile, string outfile, string password)
        {
            using (FileStream fin = File.OpenRead(infile), fout = File.OpenWrite(outfile))
            {
                int size = (int)fin.Length;
                byte[] bytes = new byte[BUFFER_SIZE];
                int read = -1;
                int value = 0;
                int outvalue = 0;
                byte[] iv = new byte[16];
                fin.Read(iv, 0, 16);
                byte[] salt = new byte[16];
                fin.Read(salt, 0, 16);
                SymmetricAlgorithm sma = CreateRijndael(password, salt);
                sma.IV = iv;
                value = 32;
                long lsize = -1;
                HashAlgorithm hasher = SHA256.Create();
                using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read), chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
                {
                    BinaryReader br = new BinaryReader(cin);
                    lsize = br.ReadInt64();
                    ulong tag = br.ReadUInt64();
                    if (FC_TAG != tag) throw new Exception("文件被破坏");
                    long numReads = lsize / BUFFER_SIZE;
                    long slack = (long)lsize % BUFFER_SIZE;
                    for (int i = 0; i < numReads; ++i)
                    {
                        read = cin.Read(bytes, 0, bytes.Length);
                        fout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value += read;
                        outvalue += read;
                    }
                    if (slack > 0)
                    {
                        read = cin.Read(bytes, 0, (int)slack);
                        fout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value += read;
                        outvalue += read;
                    }
                    chash.Flush();
                    chash.Close();
                    fout.Flush();
                    fout.Close();
                    byte[] curHash = hasher.Hash;
                    byte[] oldHash = new byte[hasher.HashSize / 8];
                    read = cin.Read(oldHash, 0, oldHash.Length);
                    if ((oldHash.Length != read) || (!CheckByteArrays(oldHash, curHash)))
                        throw new Exception("文件被破坏");
                }
                if (outvalue != lsize)
                    throw new Exception("文件大小不匹配");
            }
        }
    }
}
#endregion

#region 图像处理、图片拼接、图片裁剪、图片缩放、图上添加形状、屏幕截图、图片反色、改变图片色彩度全解
namespace util
{
    public class image_class
    {
        //image是抽象体，bitmap是具体对象对应各种格式的位图
        public static Pen 画笔 = new Pen(Brushes.Red);
        public static Bitmap splice_image(bool 横排拼接, Size 统一大小, params Bitmap[] 原图片组)
        {
            int i = 原图片组.Length;
            int 拼接宽度 = 0;
            int 拼接高度 = 0;
            int 总宽度 = 0;
            int 总高度 = 0;
            int 当前图片存放位置 = 0;
            bool 是否缩放 = true;
            Bitmap backgroudImg;
            if (统一大小.Width == 0 || 统一大小.Height == 0)
                是否缩放 = false;
            if (i == 0)
                throw new Exception("图片数不能够为0");
            if (!是否缩放)
            {
                for (int j = 0; j < i; j++)
                {
                    if (原图片组[j].Width > 拼接宽度)
                        拼接宽度 = 原图片组[j].Width;
                    if (原图片组[j].Height > 拼接高度)
                        拼接高度 = 原图片组[j].Height;
                    总宽度 = 总宽度 + 原图片组[j].Width;
                    总高度 = 总高度 + 原图片组[j].Height;
                }
            }
            else
            {
                拼接宽度 = 统一大小.Width;
                拼接高度 = 统一大小.Height;
                总高度 = 拼接高度 * i;
                总宽度 = 拼接宽度 * i;
                for (int j = 0; j < i; j++)
                {
                    if (横排拼接)
                        原图片组[j] = (Bitmap)scale_image((Image)原图片组[j], (float)拼接高度 / 原图片组[j].Height);
                    else
                        原图片组[j] = (Bitmap)scale_image((Image)原图片组[j], (float)拼接宽度 / 原图片组[j].Width);
                }
            }
            //创建要显示的图片对象,根据参数的个数设置宽度
            if (横排拼接)
                backgroudImg = new Bitmap(总宽度, 拼接高度);
            else
                backgroudImg = new Bitmap(拼接宽度, 总高度);
            Graphics g = Graphics.FromImage(backgroudImg);
            //清除画布,背景设置为白色
            g.Clear(System.Drawing.Color.White);
            if (横排拼接)
                for (int j = 0; j < i; j++)
                {
                    g.DrawImage(原图片组[j], 当前图片存放位置, 0, 原图片组[j].Width, 原图片组[j].Height);
                    当前图片存放位置 = 当前图片存放位置 + 原图片组[j].Width;
                }
            else
                for (int j = 0; j < i; j++)
                {
                    g.DrawImage(原图片组[j], 0, 当前图片存放位置, 原图片组[j].Width, 原图片组[j].Height);
                    当前图片存放位置 = 当前图片存放位置 + 原图片组[j].Height;
                }
            g.Dispose();
            return backgroudImg;
        }
        public static Bitmap cat_image(Rectangle 裁剪范围, Image 原图片)
        {
            Bitmap b = new Bitmap(原图片);
            return b.Clone(裁剪范围, System.Drawing.Imaging.PixelFormat.DontCare);
        }
        //Image 赋值，只是将指针传递，new bitmap()才真正赋值
        public static Image scale_image(Image 原图片, int 新宽, int 新高)
        {
            Image newImg = 原图片.GetThumbnailImage(新宽, 新高, new Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero); // 对原图片进行缩放 return newImg;
            GC.Collect();
            return newImg;
        }
        public static Image scale_image(Image 原图片, float 缩放比例)
        {
            Image newImg = 原图片.GetThumbnailImage((int)(原图片.Width * 缩放比例), (int)(原图片.Height * 缩放比例), new Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero); // 对原图片进行缩放 return newImg;
            GC.Collect();
            return newImg;
        }
        private static bool IsTrue() // 在 Image 类别对图片进行缩放的时候,需要一个返回 bool 类别的委托 
        { return true; }
        public static Image rect_image(Image 原图片, Point 起点, Point 终点, Pen 钢笔)
        {
            Graphics painter = Graphics.FromImage(原图片);//在图片的上一图层上画矩形
            painter.DrawRectangle(钢笔, Math.Min(起点.X, 终点.X), Math.Min(起点.Y, 终点.Y), Math.Abs(起点.X - 终点.X), Math.Abs(起点.Y - 终点.Y));
            GC.Collect();
            return 原图片;
        }
        public static Image ellipse_image(Image 原图片, Point 起点, Point 终点, Color 颜色)
        {
            画笔.Color = 颜色;
            Graphics painter = Graphics.FromImage(原图片);
            painter.DrawEllipse(画笔, Math.Min(起点.X, 终点.X), Math.Min(起点.Y, 终点.Y), Math.Abs(起点.X - 终点.X), Math.Abs(起点.Y - 终点.Y));
            GC.Collect();
            return 原图片;
        }
        public static Image rect_image2(Image 原图片, Point 起点, Point 终点, Color 颜色, int 透明度)
        {
            Graphics painter = Graphics.FromImage(原图片);
            painter.FillRectangle(new SolidBrush(Color.FromArgb(透明度, 颜色.R, 颜色.G, 颜色.B)), (int)Math.Min(起点.X, 终点.X), (int)Math.Min(起点.Y, 终点.Y), Math.Abs(起点.X - 终点.X), Math.Abs(起点.Y - 终点.Y));
            GC.Collect();
            return 原图片;
        }
        public static Image img_tailor(Point 起点, Point 终点)
        {
            Image img = new Bitmap(Math.Abs(起点.X - 终点.X), Math.Abs(起点.Y - 终点.Y));
            Graphics g = Graphics.FromImage(img);
            g.CopyFromScreen(起点, new Point(0, 0), new Size(Math.Abs(起点.X - 终点.X), Math.Abs(起点.Y - 终点.Y)));
            return img;
        }
        public static Image a_image(Image 原图片, int 颜色透明度)
        {
            Bitmap img = new Bitmap(原图片);
            using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.DrawImage(img, 0, 0);
                    for (int h = 0; h < img.Height; h++)
                    {
                        for (int w = 0; w < img.Width; w++)
                        {
                            Color c = img.GetPixel(w, h);
                            bmp.SetPixel(w, h, Color.FromArgb(颜色透明度, c.R, c.G, c.B));//色彩度最大为255，最小为0
                        }
                    }
                    GC.Collect();
                    return (Image)bmp.Clone();
                }
            }
        }
        public static Image color_image(Image 原图片)
        {
            Bitmap img = new Bitmap(原图片);
            using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    //g.DrawImage(img, 0, 0);
                    for (int h = 0; h < img.Height; h++)
                    {
                        for (int w = 0; w < img.Width; w++)
                        {
                            Color c = img.GetPixel(w, h);
                            bmp.SetPixel(w, h, Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B));
                        }
                    }
                    GC.Collect();
                    return (Image)bmp.Clone();
                }
            }
        }
        public static Image getScreen(Image 原图片, Point 起点, Point 终点)
        {
            Bitmap img = new Bitmap(原图片);
            using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    //    g.DrawImage(img, 0, 0);
                    for (int h = Math.Min(起点.Y, 终点.Y) - 1; h < Math.Max(起点.Y, 终点.Y); h++)
                    {
                        for (int w = Math.Min(起点.X, 终点.X) - 1; w < Math.Max(起点.X, 终点.X); w++)
                        {
                            Color c = img.GetPixel(w, h);
                            bmp.SetPixel(w, h, Color.FromArgb(255, c.R, c.G, c.B));//色彩度最大为255，最小为0
                        }
                    }
                    GC.Collect();
                    return (Image)bmp.Clone();
                }
            }
        }

        public static PictureBox show_scale(Image 原图片, PictureBox 原图片框, Point 缩放中心, float 缩放倍数)
        {
            Size yuan = new Size(缩放中心.X - 原图片框.Location.X, 缩放中心.Y - 原图片框.Location.Y);
            int 新宽 = (int)(原图片框.Width * 缩放倍数);
            int 新高 = (int)(原图片框.Height * 缩放倍数);
            Image 显示 = 原图片.GetThumbnailImage(新宽, 新高, new Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero);
            原图片框.Image = 显示;
            原图片框.Size = new Size(显示.Width, 显示.Height);
            原图片框.Location = new Point(缩放中心.X - (int)(yuan.Width * 缩放倍数), 缩放中心.Y - (int)(yuan.Height * 缩放倍数));
            GC.Collect();
            return 原图片框;
        }
        public static PictureBox open_image(PictureBox 原预览框, Size 图片最大显示, Point 中心位置)
        {
            Image 显示;
            Image 原图片 = 原预览框.Image;
            if (原图片.Width < 图片最大显示.Width && 原图片.Height < 图片最大显示.Height)
            {
                显示 = new Bitmap(原图片.Width, 原图片.Height);
                显示 = scale_image(原图片, 原图片.Width, 原图片.Height);
                原预览框.Image = 显示;
                原预览框.Size = new Size(原图片.Width, 原图片.Height);
                原预览框.Location = new Point(中心位置.X - 原图片.Width / 2, 中心位置.Y - 原图片.Height / 2);
            }
            if (原图片.Width > 图片最大显示.Width || 原图片.Height > 图片最大显示.Height)
            {
                float 宽高比 = (float)原图片.Width / 原图片.Height;
                if (宽高比 > 2.3)
                {
                    显示 = new Bitmap(图片最大显示.Width, (int)(图片最大显示.Width / 宽高比));
                    显示 = scale_image(原图片, 图片最大显示.Width, (int)(图片最大显示.Width / 宽高比));
                    原预览框.Size = new Size(图片最大显示.Width, (int)(图片最大显示.Width / 宽高比));
                    原预览框.Location = new Point(中心位置.X - 显示.Width / 2, 中心位置.Y - 显示.Height / 2);
                }
                else
                {
                    显示 = new Bitmap((int)(图片最大显示.Height * 宽高比), 图片最大显示.Height);
                    显示 = scale_image(原图片, (int)(图片最大显示.Height * 宽高比), 图片最大显示.Height);
                    原预览框.Size = new Size((int)(图片最大显示.Height * 宽高比), 图片最大显示.Height);
                    原预览框.Location = new Point(中心位置.X - 显示.Width / 2, 中心位置.Y - 显示.Height / 2);
                }
                原预览框.Image = 显示;
            }
            GC.Collect();
            return 原预览框;
        }
    }
}
#endregion